package com.appspot.didit.client.util;

import java.util.Date;
import com.appspot.didit.client.tag.Tag;
import com.appspot.didit.client.tag.TagManager;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Random;



public class Util {
	/*public static String removeDuplicateWhitespace(String inputStr) {
	    String patternStr = "\\s+";
	    String replaceStr = " ";
	    Pattern pattern = Pattern.compile(patternStr);
	    Matcher matcher = pattern.matcher(inputStr);
	    return matcher.replaceAll(replaceStr);
	}*/
	
	final static int MIN_FONT_SIZE = 10;   
	final static int MAX_FONT_SIZE = 30;
	public final static long MilisecondsPerDay = 86400000L;
	public static String[] splitStringInArray(String inputStr) {
	    return inputStr.split(" ");
	}
	
	public static DateTimeFormat dateHour = DateTimeFormat.getFormat("dd.MM hh:mm");
	public static DateTimeFormat hour = DateTimeFormat.getFormat("HH:mm");
	public static DateTimeFormat date = DateTimeFormat.getFormat("dd.MM.");
	public static DateTimeFormat completeDate = DateTimeFormat.getFormat("dd.MM.yyyy");
	public static DateTimeFormat month = DateTimeFormat.getFormat("MM.yyyy");
	
	@SuppressWarnings("deprecation")
	public static String getTime(Date date) {
		Date now = new Date();
		now.setHours(0);
		now.setMinutes(0);
		now.setSeconds(0);
		if(date.after(now)) {
			return "Heute " + hour.format(date);
		}
		
		Date yesterday = new Date(now.getTime() - 24*60*60*1000);
		
		if(date.after(yesterday)) {
			return "Gestern " + hour.format(date);
		}
		
		Date tby = new Date(now.getTime() - 48*60*60*1000);
		
		if(date.after(tby)) {
			return "Vorgestern " + hour.format(date);
		}
		
		return dateHour.format(date);
	}
	
	@SuppressWarnings("deprecation")
	public static String getDate(Date date1) {
		Date now = new Date();
		now.setHours(23);
		now.setMinutes(59);
		now.setSeconds(59);
		Date today = new Date(now.getTime() - 24*60*60*1000);
		if(date1.after(today)) {
			return "Heute";
		}
		
		Date yesterday = new Date(now.getTime() - 48*60*60*1000);
		
		if(date1.after(yesterday)) {
			return "Gestern";
		}
		
		Date tby = new Date(now.getTime() - 72*60*60*1000);
		
		if(date1.after(tby)) {
			return "Vorgestern";
		}
		
		return date.format(date1);
	}
	
	public static String getCompleteDate(Date date1) {
		return completeDate.format(date1);
	}
	
	public static String getMonth(Date date1) {
		return month.format(date1);
	}
	
	public static int getTagSize(Tag t) {
		int frequency = t.getItem_id().size();
		int min = 0;
		int max = TagManager.getInstance().MAX_FREQUENCY;
		System.out.println(t.getTagname() + " " + frequency + " " + max);
		if(frequency == max) {
			return MAX_FONT_SIZE;
		}
		if(frequency == min) {
			return MIN_FONT_SIZE;
		}
		double weight = (double)(frequency - min) / (double)(max - min);
		return MIN_FONT_SIZE + (int)Math.round((MAX_FONT_SIZE - MIN_FONT_SIZE) * weight);
	}
	
	public static String generateKey(int length) {
		int codelen = length;
		String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
		StringBuffer sb = new StringBuffer(codelen);
		for (int n = 0; n < codelen; n++) {
			int i = Random.nextInt(chars.length());
			sb.append(chars.charAt(i));
		}
		return sb.toString();
	}
	
	public static String generateKey() {
		return "1234567890";//generateKey(10);
	}
	
	public static String encrypt(String value, String key) {
		return key;
	}
	
	public static String decrypt(String value, String key) {
		return value;
	}
	
	public static double getHoursFromIntegers(int hour, int minute) {
		if(hour < 0 || minute < 0) {
			return 0.0;
		}
		double result = 0.0;
		result += hour;
		result += (double)minute / 60.0;
		result = (int)(result * 100)/100.0;

		return result;
	}
	
	public static int getMinutesFromDouble(double hours) {
		if(hours < 0) {
			return 0;
		}
		int result = 0;
		double minutes = (hours - (int)hours) * 60.0 + 0.5;
		result = (int)minutes;
		return result;
	}
}
